//
//  SearchView.m
//  Cat
//
//  Created by lushitong on 2019/1/13.
//  Copyright © 2019年 lushitong. All rights reserved.
//

#import "SearchView.h"
#import "QKShareLiveSheetManager.h"
#import "SearchViewController.h"
#import "AppDelegate.h"
#import "MainViewController.h"

@interface SearchView()

@property (nonatomic, strong) UIView *operView;
@property (nonatomic, strong) UIImageView *topInfoImageView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) UIView *line2View;
@property (nonatomic, strong) UIButton *cannenButton;
@property (nonatomic, strong) UIButton *searchButton;

@end

@implementation SearchView

- (instancetype)initWithFrame:(CGRect)frame {
	self = [super initWithFrame:frame];
	if (self) {
		self.userInteractionEnabled = YES;
		[self loadAllSubViews];
		UIPasteboard* pasteboard = [UIPasteboard generalPasteboard];
		self.titleLabel.text = pasteboard.string;
	}
	return self;
}

- (void)loadAllSubViews {
	[self addSubview:self.operView];
	
	[self.operView addSubviews:@[self.topInfoImageView,self.titleLabel,self.lineView,self.line2View,self.cannenButton,self.searchButton]];
	
	[self.operView mas_makeConstraints:^(MASConstraintMaker *make) {
		make.size.equalTo(CGSizeMake(ScreenWidth - 2* kQKFitModuleDecimal(61), kQKFitModuleDecimal(75 + 150)));
		make.center.equalTo(self);
	}];

	[self.topInfoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
		make.top.and.left.right.equalTo(self.operView);
		make.height.equalTo(@(kQKFitModuleDecimal(75)));
	}];

	[self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
		make.top.equalTo(self.topInfoImageView.mas_bottom).offset(kQKFitModuleDecimal(20));
		make.left.equalTo(self.operView.mas_left).offset(kQKFitModuleDecimal(17));
		make.right.equalTo(self.operView.mas_right).offset(-kQKFitModuleDecimal(17));
		make.bottom.lessThanOrEqualTo(self.operView.mas_bottom).offset(-60);
	}];

	[self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.and.right.equalTo(self.operView);
		make.height.equalTo(@(kQKFitModuleDecimal(1.0)));
		make.top.equalTo(self.topInfoImageView.mas_bottom).offset(kQKFitModuleDecimal(104));
	}];

	[self.line2View mas_makeConstraints:^(MASConstraintMaker *make) {
		make.width.equalTo(@(kQKFitModuleDecimal(1.0)));
		make.centerX.equalTo(self.operView.mas_centerX);
		make.top.equalTo(self.lineView.mas_bottom);
		make.bottom.equalTo(self.operView.mas_bottom);
	}];

	[self.cannenButton mas_makeConstraints:^(MASConstraintMaker *make) {
		make.size.equalTo(CGSizeMake((ScreenWidth - 2* kQKFitModuleDecimal(60)) /2, kQKFitModuleDecimal(35)));
		make.left.equalTo(self.operView.mas_left);
		make.top.equalTo(self.lineView.mas_bottom);
	}];

	[self.searchButton mas_makeConstraints:^(MASConstraintMaker *make) {
		make.size.equalTo(CGSizeMake((ScreenWidth - 2* kQKFitModuleDecimal(60)) /2, kQKFitModuleDecimal(35)));
		make.top.equalTo(self.lineView.mas_bottom);
		make.left.equalTo(self.cannenButton.mas_right);
	}];
	
}

- (void)searchAction {
	[QKShareLiveSheetManager dismissSearch];
	UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
	SearchViewController *seVC = [[SearchViewController alloc] init];
	seVC.keyWord = pasteboard.string;
	QKBaseNavigationController *navCtrl = [SearchView currentTabbarSelectedNavigationController];
	[navCtrl pushViewController:seVC animated:YES];
	pasteboard.string = @"";
}

- (void)cannenAction {
	UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
	pasteboard.string = @"";
	[QKShareLiveSheetManager dismissSearch];
}

+ (QKBaseNavigationController *)currentTabbarSelectedNavigationController {
	AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
	QKBaseTabBarController *tabbarController = (MainViewController *)appDelegate.window.rootViewController;
	QKBaseNavigationController *selectedNV = (QKBaseNavigationController *)tabbarController.selectedViewController;
	if ([selectedNV isKindOfClass:[QKBaseNavigationController class]]) {
		return selectedNV;
	}
	return nil;
}

- (UIView *)operView {
	if (!_operView) {
		_operView = [[UIView alloc] init];
		_operView.backgroundColor = [UIColor whiteColor];
	}
	return _operView;
}

- (UIImageView *)topInfoImageView {
	if (!_topInfoImageView) {
		_topInfoImageView = [[UIImageView alloc] init];
		_topInfoImageView.image = [UIImage imageNamed:@"Bitmap"];
	}
	return _topInfoImageView;
}

- (UILabel *)titleLabel {
	if (!_titleLabel) {
		_titleLabel = [UILabel labelWithFont:[UIFont systemFontOfSize:kQKFitFont(14)] textColor:COLOR_WITH_HEX(0x4C4947)];
		_titleLabel.numberOfLines = 0;
	}
	return _titleLabel;
}

- (UIView *)lineView {
	if (!_lineView) {
		_lineView = [[UIView alloc] init];
		_lineView.backgroundColor = COLOR_WITH_HEX(0xEEEBEB);
	}
	return _lineView;
}

- (UIView *)line2View {
	if (!_line2View) {
		_line2View = [[UIView alloc] init];
		_line2View.backgroundColor = COLOR_WITH_HEX(0xEEEBEB);
	}
	return _line2View;
}

- (UIButton *)searchButton {
	if (!_searchButton) {
		_searchButton = [UIButton buttonWithTitle:@"搜索" color:COLOR_WITH_HEX(0xFFC236) font:[UIFont systemFontOfSize:kQKFitFont(14)]];
		[_searchButton addTarget:self action:@selector(searchAction) forControlEvents:UIControlEventTouchUpInside];
	}
	return _searchButton;
}

- (UIButton *)cannenButton {
	if (!_cannenButton) {
		_cannenButton = [UIButton buttonWithTitle:@"取消" color:COLOR_WITH_HEX(0xCDC7C4) font:[UIFont systemFontOfSize:kQKFitFont(14)]];
		[_cannenButton addTarget:self action:@selector(cannenAction) forControlEvents:UIControlEventTouchUpInside];
	}
	return _cannenButton;
}
@end
